$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/classtime/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '课次名称', name: 'name', index: 'name', width: 80 }, 			
			// { label: '题目id,多个用逗号隔开', name: 'questionids', index: 'questionids', width: 80 },
            { label: '题目图片', name: 'questionids', index: 'questionids', width: 80 , formatter: function(value, options, row) {
                    var url = "";
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/question/infoName/"+row.questionids,
                        // data:{'questionsId':row.questionids},
                        success: function(r){
                            console.log(r.data)
                            if (null!=r.data) {
                                var strarr = (r.data).split(",");
                                for (var i=0;i<strarr.length;i++) {
                                    url+='<img  style="width: 60px" src="' + strarr[i] + '" onmouseover ="changeImg(this)" onmouseout="changeImgOut()">';
                                }
                            }
                        }
                    });
                    return url;
                }},
			{ label: '添加时间', name: 'createtime', index: 'createtime', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
function changeImg (img) {
    $(".modal-backdrop").remove();
    $('#bigImg').html("");
    $("#imgModal").modal();
    $("#bigImg").append('<img  style="width: 380px;height: 380px" src="' + img.src + '">');
}
function changeImgOut () {
    $('#bigImg').html("");
    $("#imgModal").modal("hide");
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		classtime: {},
        question:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.classtime = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                vm.classtime.createtime = $("#createtime").val()
                var url = vm.classtime.id == null ? "sys/classtime/save" : "sys/classtime/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.classtime),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
        changeImg: function(img) {
		    alert("22");
            img.src = '<img  style="width: 250px" src="' + strarr[i] + '">';
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/classtime/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        addQuestionids: function () {
            $("#jqGridAdd").jqGrid({
                url: baseURL + 'sys/question/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 100, key: true },
                    { label: '图片链接', name: 'nameimg', index: 'nameimg', width: 300 , formatter(value, options, row) {
                            return '<img  style="width: 100px" src="' + value + '">';
                        }}
                ],
                viewrecords: true,
                height: 385,
                width:  680,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager1",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            $('#myModal').modal();
        },
        addQuestions: function (event) {
            var grid = $("#jqGridAdd");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }
            var rows = grid.getGridParam("selarrrow");

            var temp = "";
            $("#questionids").html("");
            for(var i=0;i<rows.length;i++){
                var obj = {};
                var isExits = false;
                if(vm.question.length>0){
                    for(var j=0;j<vm.question.length;j++){
                        if(rows[i]==vm.question[j].id){
                            isExits = true;
                        }
                    }
                    if(!isExits){
                        obj.id = rows[i];
                        obj.nameimg = grid.getRowData(rows[i]).nameimg;
                        vm.question.push(obj);

                    }

                }else{
                    obj.id = rows[i];
                    obj.nameimg = grid.getRowData(rows[i]).nameimg;
                    vm.question.push(obj);


                }

            }
            for(var k=0;k<vm.question.length;k++){
                // temp +="<a class='btn' style='color: #333;border-color: #ccc;cursor:default;' " +
                //     "id='"+vm.question[k].id+"'>"+vm.question[k].nameimg+"</a>" +
                //     "<img src='../../statics/img/ico15-close.png' onclick='vm.removeQuestions("+vm.question[k].id+")'" +
                //     "style='z-index:12;width:13px;height:13px;margin-left:-14px;margin-top:-15px;cursor: pointer; ' />&nbsp;&nbsp;";
                temp+='<img  style="width: 50px" src="' + vm.question[k].nameimg + '">&nbsp;&nbsp;'
            }
            vm.classtime.questionids='';
            for(var j=0;j<vm.question.length;j++){
                vm.classtime.questionids+=vm.question[j].id+","
            }
            str = vm.classtime.questionids.substring(0, vm.classtime.questionids.lastIndexOf(','));
            vm.classtime.questionids = str;
            $("#questionids").append(temp);
            console.log(temp)
            console.log(vm.question)
            $('#myModal').modal("hide");
        },
        removeQuestions: function (id) {
            var temp = "";
            if(vm.question.length==1){
                alert("至少选择一个题目");
                return;
            }
            $("#questionids").html("");
            var classtimetemp = [];
            for(var i=0;i<vm.question.length;i++){
                var obj = {};

                if(vm.question[i].id !=id){
                    obj.id = vm.question[i].id;
                    obj.name = vm.question[i].name
                    classtimetemp.push(obj);

                    temp +="<a class='btn' style='color: #333;border-color: #ccc;cursor:default;' " +
                        "id='"+vm.question[i].id+"'>"+vm.question[i].name+"</a>" +
                        "<img src='../../statics/img/ico15-close.png' onclick='vm.removeQuestions("+vm.question[i].id+")'" +
                        "style='z-index:12;width:13px;height:13px;margin-left:-14px;margin-top:-15px;cursor: pointer; ' />&nbsp;&nbsp;";
                }

            }
            vm.question = classtimetemp;
            $("#questionids").append(temp);
        },
		getInfo: function(id){
			$.get(baseURL + "sys/classtime/info/"+id, function(r){
                vm.classtime = r.classtime;
                $("#questionids").text("");
                var urlImg="";
                $.ajax({
                    type: "POST",
                    async:false,
                    url: baseURL + "sys/question/infoName/"+vm.classtime.questionids,
                    success: function(r){
                        console.log(r.data)
                        if (null!=r.data) {
                            var strarr = (r.data).split(",");
                            for (var i=0;i<strarr.length;i++) {
                                urlImg+='<img  style="width: 280px;height: 280px" src="' + strarr[i] + '">';
                            }
                        }
                    }
                });

                $("#questionids").append(urlImg);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});